﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_Notice_Default : System.Web.UI.Page
{

	public string SqlClass = "";
    public int sid=-1;
    string[] strqx;
    string checksid, checksql;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"]==null)
		{
			Response.Write("<script>alert('你没有管理员权限，禁止访问！');history.back();</script>");
            Response.End();
		}
        strqx = Session["limitcheck"].ToString().Split(',');
		if (!Page.IsPostBack)
        {
            Socut.Reader searchReader = new Socut.Reader("select id,smallclassname from web_music_smallclass order by id");
            DropDownList1.Items.Add(new ListItem("请选择文章分类", "0"));
            while (searchReader.Read())
            {
                string strsid = searchReader["id"].ToString();

                DropDownList1.Items.Add(new ListItem(searchReader["smallclassname"].ToString().Replace("&nbsp;", " "), searchReader["id"].ToString()));
                //DropDownList1.Items.Add(new ListItem(searchReader["smallclassname"].ToString().Replace("&nbsp;"," "), searchReader["ID"].ToString()));
            }
            searchReader.Close();
			//Session["sid"] = "";
            //DataSet dss = Socut.Data.ExecuteDataSet("select smallclassname,id from web_music_smallclass order by id");
            
			//this.DropDownList1.DataSource = dss.Tables[0].DefaultView;
            //this.DropDownList1.DataTextField = "smallclassname";
			//this.DropDownList1.DataTextField = dss.Tables[0].Rows[0][0].ToString().Replace("&nbsp;"," ");
            //this.DropDownList1.DataValueField = "id";
            //this.DropDownList1.DataBind();
            ShowData();
        }

    }
    private void ShowData()
    {
         //Response.Write(Session["sid"]);
		 //Response.End();

        DataTable dts = Socut.Data.ExecuteDataSet("select id,smallclassid from web_music").Tables[0];
        if (dts.Rows.Count > 0)
        {
            for (int j = 0; j < dts.Rows.Count; j++)
            {

                //Response.Write(dts.Rows[j]["id"]);
                //Response.End();
                checksid = dts.Rows[j]["smallclassid"].ToString();
                if (check_class(strqx, checksid))
                {
                    checksql = "and smallclassid=" + checksid;
                
                }
            }
            

        }


		 if (Session["sid"]!=null && Session["key"]!=null)
            {
                SqlClass = "and smallclassid=" + Session["sid"] + " and (title like '%" + Session["key"] + "%' or Content like '%" + Session["key"] + "%')";
            }
         if (Session["sid"]==null && Session["key"]!=null)
            {
                SqlClass = "and (title like '%" + Session["key"] + "%' or Content like '%" + Session["key"] + "%')" + checksql;
            }
		 if (Session["sid"]==null && Session["key"]==null)
            {
                SqlClass = "" + checksql;
            }
		 if (Session["sid"]!=null && Session["key"]==null)
            {
                SqlClass = "and smallclassid=" + Session["sid"] + "";
            }
        if (sid != -1)
        {
            SqlClass = "and smallclassid=" + sid;
        }
        if (SqlClass != "")
            AspNetPager1.RecordCount = (int)Socut.Data.ExecuteScalar("select count(*) from web_music where 1=1 " + SqlClass);
        else
            AspNetPager1.RecordCount = (int)Socut.Data.ExecuteScalar("select count(*) from web_music");
        string ExecSQL="select web_music.id,web_music.AddTime,web_music.Title,web_music_smallclass.smallclassname from web_music,web_music_smallclass where web_music.smallclassid=web_music_smallclass.id " + SqlClass + " order by web_music.id desc";
        DataSet ds = new DataSet();
        ds=Socut.Data.ExecuteDataSet(ExecSQL, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        ShowData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string ids = Request.Form["Ccheck"];
        if (string.IsNullOrEmpty(ids))
        {
            CommonFunction.ShowMessage("请选择要删除的项");
        }
        else
        {
            Socut.Data.ExecuteNonQuery("DELETE from web_music where id in (" + ids + ")");
            CommonFunction.ShowMessage("删除成功","default.aspx");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(TextBox1.Text))
        {
            CommonFunction.ShowMessage("请输入关键字再进行搜索");
        }
        else
        {
            Session["key"]=TextBox1.Text.Trim();
			ShowData();
			//Response.Redirect("Default.aspx?Key=" + CommonFunction.UrlEncode(TextBox1.Text));
        }
    }
	
	protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ss = DropDownList1.SelectedValue;
        sid = Convert.ToInt32(ss);
        Session["sid"] = sid;
        ShowData();
	}

    private bool check_class(string[] ksarry, string ksid)
    {
        for (int i = 0; i < ksarry.Length; i++)
        {
            if (ksarry[i] == ksid)
            {
                return true;
            }
        }
        return false;
    }

}
